﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports System.Data.OleDb
Imports System
Imports System.Globalization
Partial Class Interfaz_ModuloPlanificacion_PopUpPlanificacion1
    Inherits System.Web.UI.Page

    Dim CL As Logica.ModuloPlanificacion.ControladorLogica
    Dim CLAdmin As Logica.ModuloAdministracion.ControladorLogica
    Dim nplani_ppal As Planificacion
    Dim dtDatosPlanificacion As New DataTable


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CL = Logica.ModuloPlanificacion.ControladorLogica.getInstance
        CLAdmin = Logica.ModuloAdministracion.ControladorLogica.getInstance

        nplani_ppal = DirectCast(CL.ObtenerPlanificacionPrincipal(Request("NplanPpal").ToString, 2).Item(0), Planificacion)

        Me.LblNPlanificacion.Text = nplani_ppal.Id

        Dim presu As ArrayList = Me.CLAdmin.ObtenerPresupuestoCriterio(DirectCast(CL.ObtenerOT(nplani_ppal.Ot_fk, 2).Item(0), OrdenTrabajo).FkPresupuesto)
        Me.LblNOt.Text = nplani_ppal.Ot_fk & "-" & DirectCast(presu.Item(0), Presupuesto).atencion

        If Not Page.IsPostBack Then
            Try

                dtDatosPlanificacion.Columns.Add(New DataColumn("MARCA"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("ID"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("CANTIDAD"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("DESCRIPCION"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("KGUNI"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("OBSERVACION"))
                dtDatosPlanificacion.Columns.Add(New DataColumn("PLANO"))

                Me.obtenerPlanificacion_M1()
                Me.GridEditarPlanificacion.DataBind()
                Me.GridEditarPlanificacion.DataSource = Me.dtDatosPlanificacion
                Me.UpdateGridChekBoxPlanificado()
            Catch
            End Try
        Else
        End If
        Me.GridEditarPlanificacion.DataSource = dtDatosPlanificacion
    End Sub


#Region "Obtener"

    Public Sub obtenerPlanificacion_M1()
        Me.dtDatosPlanificacion.Clear()
        Dim array As ArrayList = CL.ObtenerPlanificacionM1(Request("NplanPpal"), 3)

        If array.Count > 0 Then

            For i As Integer = 0 To array.Count - 1
                Dim drDatos As DataRow = dtDatosPlanificacion.NewRow
                Dim pm1 As Planificacion_M1 = array.Item(i)

                drDatos.Item("MARCA") = pm1.marca
                drDatos.Item("ID") = pm1.id
                drDatos.Item("CANTIDAD") = pm1.Cantidad
                drDatos.Item("DESCRIPCION") = pm1.Descripcion
                drDatos.Item("KGUNI") = pm1.Kguni
                drDatos.Item("OBSERVACION") = pm1.Observacion
                drDatos.Item("PLANO") = pm1.Plano

                dtDatosPlanificacion.Rows.Add(drDatos)

            Next
            Me.GridEditarPlanificacion.DataSource = dtDatosPlanificacion
            Me.GridEditarPlanificacion.DataBind()

            Me.LblMensaje.Text = ("Los Datos han Sido Cargados Exitosamente.")
            Me.LblMensaje.ForeColor = Drawing.Color.Blue
        Else
            Me.LblMensaje.Text = ("La Planificación No cuenta Con Marcas Creadas.")
            Me.LblMensaje.ForeColor = Drawing.Color.Red
        End If

    End Sub

#End Region

#Region "Ingresar"

    Public Sub ingresarPm1()
        If Me.validarIngreso Then
            Dim pm1 As Planificacion_M1 = New Planificacion_M1(Me.TxtmarcaFab.Text, Me.TxtCantidad.Text, Me.TxtDescMarcaFab.Text, Me.TxtKgUni.Text, Me.TxtObsMarcaFab.Text, Me.TxtPlanoFab.Text, False, "0", Me.LblNPlanificacion.Text)
            CL.ingresarPlanificacionM1(pm1)
            Me.LblMensaje.Text = "Los Datos Han Sido Ingresados Exitosamente"
            Me.LblMensaje.ForeColor = Drawing.Color.Blue
            LimpiarCampos()
        Else
            Me.LblMensaje.Text = "Para Ingresar Necesita Completar Todos los Datos"
            Me.LblMensaje.ForeColor = Drawing.Color.Red
        End If

    End Sub

#End Region

#Region "Actualizar"

    Public Sub ActualizarPm1(ByVal estado As Boolean, ByVal id As Integer)

        Try
            CL.UpdateEstadoPm1(estado, id)
        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try

    End Sub

#End Region
   
#Region "utilidades"

    Private Sub FuncionAbrirVentana(ByVal url As String)
        ClientScript.RegisterStartupScript(Me.GetType(), "AbrirVentana;", "window.open(" & url & ",'','width=950,height=700,menubar=1,resizable=1,scrollbars=1');", True)
    End Sub

    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Public Function validarIngreso() As Boolean
        If Me.TxtCantidad.Text <> "" And Me.TxtDescMarcaFab.Text <> "" And Me.TxtKgUni.Text <> "" And Me.TxtmarcaFab.Text <> "" And Me.TxtObsMarcaFab.Text <> "" And Me.TxtPlanoFab.Text <> "" Then
            Return True
        End If
        Return False
    End Function

    Public Sub LimpiarCampos()
        Me.TxtCantidad.Text = String.Empty
        Me.TxtDescMarcaFab.Text = String.Empty
        Me.TxtKgUni.Text = String.Empty
        Me.TxtmarcaFab.Text = String.Empty
        Me.TxtObsMarcaFab.Text = String.Empty
        Me.TxtPlanoFab.Text = String.Empty
    End Sub

    Public Sub UpdateGridChekBoxPlanificado()
        With Me.GridEditarPlanificacion
            For i As Integer = 0 To .Rows.Count
                Dim row As GridViewRow = .Rows(i)
                Dim buscado As Planificacion_M1 = DirectCast(CL.ObtenerPlanificacionM1(row.Cells(1).Text, 2).Item(0), Planificacion_M1)
                If buscado.Planificado = True Then
                    Dim c As CheckBox = row.Cells(5).FindControl("CBListo")
                    c.Checked = True
                End If
            Next
        End With
    End Sub

#End Region
    
#Region "Eventos"

    Protected Sub GridItems_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridEditarPlanificacion.RowCommand
        Try
            Dim accion As String = e.CommandName.ToString
            Dim url As String = ""
            Dim index As String = CInt(e.CommandArgument)
            Dim subProy As String = Me.GridEditarPlanificacion.Rows(index).Cells(1).Text
            Dim fila As GridViewRow = Me.GridEditarPlanificacion.Rows(index)

            If accion = "EditarPlanificacion" Then
                url = """PopUpPlanificacion2.aspx?Nplan2=" & subProy & """"
                FuncionAbrirVentana(url)
            End If
        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try
    End Sub

    Protected Sub CBListo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim cb As CheckBox = DirectCast(sender, CheckBox)
        Dim row As GridViewRow = DirectCast(cb.NamingContainer, GridViewRow)

        If cb.Checked = True Then
            Me.ActualizarPm1(True, row.Cells(1).Text)
            Me.Mensaje("La Marca Seleccionada se ha Marcado como Planificada")
        Else
            Me.ActualizarPm1(False, row.Cells(1).Text)
            Me.Mensaje("La Marca Seleccionada se ha Marcado como NO Planificada")
        End If

    End Sub

#End Region

#Region "Botones"

    Protected Sub BtnIngresarPlanificacion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnIngresarPlanificacion.Click

        ingresarPm1()
        Me.dtDatosPlanificacion.Clear()
        Me.GridEditarPlanificacion.DataSource = Me.dtDatosPlanificacion

        dtDatosPlanificacion.Columns.Add(New DataColumn("MARCA"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("ID"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("CANTIDAD"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("DESCRIPCION"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("KGUNI"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("OBSERVACION"))
        dtDatosPlanificacion.Columns.Add(New DataColumn("PLANO"))
        obtenerPlanificacion_M1()
        Me.UpdateGridChekBoxPlanificado()
    End Sub

#End Region

End Class
